#!/usr/bin/env python

# pita: An invisible audio player
# Copyright (C) 2006-2007 Austin Bingham
#
# This program is free software# you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation# either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# You can reach the author at:
#     austin.bingham@gmail.com

import pygtk
pygtk.require('2.0')
import gtk, gobject

import pita.client_util as cutil
import pita, pita.app

def playlist_row_to_color(treeviewcolumn, cell_renderer, model, itr):
    '''
    a cell_data_func that sets a cells background to cyan if the
    second element in the specified row is true. Basically, this is being
    used to highlight the current song.
    '''
    curr = bool(model.get_value(itr, 1))
    if curr:
        color = 'cyan'
    else:
        color = 'white'
        
    cell_renderer.set_property('background', color)

class PitaStat:

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def construct_tree(self, server):
        # create and populate the tree store
        self.treestore = gtk.TreeStore(str, bool)

        playlist = cutil.net_to_playlist(server.get_playlist())
        for idx in range(len(playlist)):
            self.treestore.append(None, [playlist[idx],
                                         idx == playlist.current])

        # create tree view
        self.treeview = gtk.TreeView(self.treestore)
        self.treeview.get_selection().set_mode(gtk.SELECTION_NONE)

        # create column for viewing playlist files
        self.file_column = gtk.TreeViewColumn('Playlist')
        self.treeview.append_column(self.file_column)

        # create cell renderer for file column
        self.file_cell_renderer = gtk.CellRendererText()
        self.file_column.pack_start(self.file_cell_renderer, True)
        self.file_column.add_attribute(self.file_cell_renderer, 'text', 0)
        self.file_column.set_cell_data_func(self.file_cell_renderer, playlist_row_to_color)

        # make the tree searchable
        self.treeview.set_search_column(0)

        # make tree reorderable
        self.treeview.set_reorderable(True)

        return self.treeview

    def construct_attr_label(self, func, attr):
        if func():
            text = attr
        else:
            text = 'Not %s' % attr
        layout = gtk.HBox()
        layout.pack_start(gtk.Label(text), False)
        return layout

    def window_key_press_handler(self, widget, event):
        if event.keyval == gtk.gdk.keyval_from_name("Escape"):
            self.destroy(self)

    def __init__(self, server):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_border_width(10)
        self.window.set_title("PitaStat")

        # self.window.add(self.construct_tree())
        self.layout = gtk.VBox()
        # self.layout.pack_start(gtk.Label('this is a test'))
        self.layout.pack_start(self.construct_attr_label(server.is_paused, 'Paused'))
        self.layout.pack_start(self.construct_attr_label(server.is_random, 'Random'))
        self.layout.pack_start(self.construct_attr_label(server.is_looping, 'Looping'))
        self.layout.pack_start(self.construct_tree(server))
        self.window.add(self.layout)

        self.window.connect("key_release_event", self.window_key_press_handler)
        self.window.connect("delete_event", self.destroy)

        self.window.show_all()

    def main(self):
        gtk.main()

class Application(pita.app.ClientApplication):
    def __init__(self):
        pita.app.ClientApplication.__init__(self, 'pitastat')

    def run(self, args):
        server = cutil.get_server_proxy_or_die(pita.conf)
        
        pstat = PitaStat(server)
        pstat.main()

if __name__ == "__main__":
    app = Application()
    app.start()
